package com.ruyuan.eshop.construction.builder;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.construction.domain.entity.DraftImgDO;
import com.ruyuan.eshop.construction.domain.entity.DraftMainDO;
import com.ruyuan.eshop.construction.domain.entity.ItemInfoDO;
import com.ruyuan.eshop.construction.domain.entity.ItemVideoImgDO;
import com.ruyuan.eshop.common.enums.construction.AuditStatusEnum;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 全量草稿数据
 *
 * @author zhonghuashishan
 */
public class ProductDraftBuilder {

    /**
     * 全量商品数据
     */
    private FullProductData fullProductData;

    /**
     * 全量草稿数据
     */
    private FullDraftData fullDraftData;

    public ProductDraftBuilder(FullProductData fullProductData) {
        this.fullProductData = fullProductData;
        this.fullDraftData = new FullDraftData();
    }

    public ProductDraftBuilder buildDraftMain(Integer auditType) {
        DraftMainDO draftMainDO = new DraftMainDO();
        ItemInfoDO itemInfoDO = fullProductData.getItemInfoDO();
        // TODO cspuId
        draftMainDO.setCspuId(1L);

        draftMainDO.setItemId(itemInfoDO.getItemId());
        draftMainDO.setDraftType(itemInfoDO.getItemType());
        draftMainDO.setDraftName(itemInfoDO.getItemName());
        draftMainDO.setDraftStatus(itemInfoDO.getItemStatus());
//        draftMainDO.setSaleAreaJson();
//        draftMainDO.setBasePrice();
        draftMainDO.setBrandId(itemInfoDO.getBrandId());
        draftMainDO.setChannel(itemInfoDO.getChannel());
        draftMainDO.setSellerType(itemInfoDO.getSellerType());
        draftMainDO.setRecommend(itemInfoDO.getRecommend());
//        draftMainDO.setHighlights();
//        draftMainDO.setSellingPoint();
//        draftMainDO.setBarCode();
        draftMainDO.setFirstCategoryId(itemInfoDO.getFirstCategoryId());
        draftMainDO.setSecondCategoryId(itemInfoDO.getSecondCategoryId());
        draftMainDO.setThirdCategoryId(itemInfoDO.getThirdCategoryId());
//        draftMainDO.setTicketId();
        draftMainDO.setTicketStatus(AuditStatusEnum.UNAUDITED.getCode());
        draftMainDO.setTicketType(auditType);
        draftMainDO.setRejectReason("提交审核");
        draftMainDO.setFeatures(JSON.toJSONString(fullProductData));
        // 草稿表版本号是正式数据版本号加1
        draftMainDO.setVersionId(itemInfoDO.getVersionId() + 1);
        draftMainDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

        draftMainDO.setCreateTime(new Date());
        draftMainDO.setCreateUser(itemInfoDO.getUpdateUser());
        draftMainDO.setUpdateTime(new Date());
        draftMainDO.setUpdateUser(itemInfoDO.getUpdateUser());

        fullDraftData.setDraftMainDO(draftMainDO);
        return this;
    }


    public ProductDraftBuilder buildDraftImgList() {
        List<DraftImgDO> draftImgDOS = new ArrayList<>(16);
        List<ItemVideoImgDO> itemVideoImgDOList = fullProductData.getItemVideoImgDOList();
        if (CollectionUtils.isEmpty(itemVideoImgDOList)) {
            return this;
        }
        for (ItemVideoImgDO itemVideoImgDO : itemVideoImgDOList) {
            DraftImgDO draftImgDO = new DraftImgDO();
            draftImgDO.setImgUrl(itemVideoImgDO.getContentUrl());
            draftImgDO.setImgSort(itemVideoImgDO.getContentSort());
            draftImgDO.setChannel(itemVideoImgDO.getChannel());

            draftImgDO.setSellerType(fullProductData.getItemInfoDO().getSellerType());

            draftImgDO.setCreateTime(new Date());
            draftImgDO.setUpdateTime(new Date());

            draftImgDOS.add(draftImgDO);
        }
        fullDraftData.setDraftImgDOS(draftImgDOS);
        return this;
    }


    public FullDraftData build() {
        return this.fullDraftData;
    }
}
